package com.zh.api.controller.operation;

import com.zh.common.annotation.Log;
import com.zh.common.base.BaseController;
import com.zh.common.domain.DictDO;
import com.zh.common.utils.*;
import com.zh.operation.domain.*;
import com.zh.operation.domain.OperationGoodsTakeDOVO;
import com.zh.operation.domain.OperationGoodsTakeDO;
import com.zh.operation.service.BasicInfoService;
import com.zh.operation.service.OperationGoodsService;
import com.zh.operation.service.OperationGoodsTakeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Controller
@Api("资产【物品领用表】")
public class OperationGoodsTakeController extends BaseController {
	@Autowired
	private OperationGoodsTakeService operationGoodsTakeService;
	@Autowired
	private OperationGoodsService operationGoodsService;
	@Autowired
	private BasicInfoService basicInfoService;

	@Log("物品领用列表")
	@ResponseBody
	@RequestMapping(value = "listOperationGoodsTake", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ResultVO listOperationGoodsTake(@RequestBody Map<String, Object> params){
		if(CheckEmptyUtil.isEmpty(params)){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		//查询列表数据
        Query query = new Query(params);
		List<OperationGoodsTakeDOVO> goodsTakeList = operationGoodsTakeService.listAllData(query);
        //      查询领用人
		for (OperationGoodsTakeDOVO nu:goodsTakeList){
			BasicInfoDO basicInfoDO = basicInfoService.get(nu.getUserId());
			if (basicInfoDO !=null && basicInfoDO.getNusName()!=null) {
				nu.setName(basicInfoDO.getNusName());
			}else{
				continue;
			}
		}
		int total = operationGoodsTakeService.count(query);
		PageUtils pageUtils = new PageUtils(goodsTakeList, total);
		return ResultVO.success(pageUtils);
	}

	@Log("添加物品领用")
	@ResponseBody
	@RequestMapping(value = "addOperationGoodsTake", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ResultVO addOperationGoodsTake(@RequestBody Map<String, Object> params, Model model){
		List<OperationGoodsDOVO> list = operationGoodsService.list(params);
		List<DictDO> listDict = operationGoodsTakeService.listGoodsStatus();
		model.addAttribute("listDict",listDict);
		return ResultVO.success(list);
	}

	@Log("编辑物品领用")
	@ResponseBody
	@RequestMapping(value = "editOperationGoodsTakeById", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ResultVO editOperationGoodsTakeById(@PathVariable("id") String id, Model model, @RequestParam Map<String, Object> params){
		List<OperationGoodsDOVO> list = operationGoodsService.list(params);
		model.addAttribute("list",list);
		OperationGoodsTakeDO goodsTake = operationGoodsTakeService.get(id);
		model.addAttribute("goodsTake", goodsTake);
		List<DictDO> listDict = operationGoodsTakeService.listGoodsStatus();
		model.addAttribute("listDict",listDict);
	    return ResultVO.success();
	}

	@Log("保存物品领用")
	@ResponseBody
	@RequestMapping(value = "saveOperationGoodsTake", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ResultVO saveOperationGoodsTake(@RequestBody OperationGoodsTakeDO goodsTake){
		if(goodsTake==null){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		goodsTake.setId(new SimpleDateFormat("yyyyMMddHHmmssSS").format(new Date())+(int)((Math.random()*9+1)*1000));
		goodsTake.setCreateBy(getUserId().toString());
		goodsTake.setUpdateBy(getUserId().toString());
		goodsTake.setUpdateDate(new Date());
		goodsTake.setCreateDate(new Date());
		goodsTake.setDelFlag("0");
		if(operationGoodsTakeService.save(goodsTake)>0){
			return ResultVO.success();
		}
		return ResultVO.error();
	}
	@Log("更新物品领用")
	@ResponseBody
	@RequestMapping(value = "updateOperationGoodsTakeById", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ResultVO updateOperationGoodsTakeById(@RequestBody OperationGoodsTakeDO goodsTake){
		if(goodsTake==null || CheckEmptyUtil.isEmpty(goodsTake.getId())){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		operationGoodsTakeService.update(goodsTake);
		return ResultVO.success();
	}

	@PostMapping( "/return")
	@ResponseBody
	@ApiOperation("归还操作")
	public R returnGoods(String id) {
		operationGoodsTakeService.returnGoods(id);
		return R.ok("归还成功");
	}

	@Log("删除物品领用")
	@ResponseBody
	@RequestMapping(value = "removeOperationGoodsTakeById", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ResultVO removeOperationGoodsTakeById(@RequestBody OperationGoodsTakeDO goodsTake){
		if(goodsTake==null || CheckEmptyUtil.isEmpty(goodsTake.getId())){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		if(operationGoodsTakeService.remove(goodsTake.getId())>0){
		return ResultVO.success();
		}
		return ResultVO.error();
	}



	//获取条形码
	@ResponseBody
	@PostMapping("/getUuid/{id}")
	@ApiOperation("获取条形码")
	public String getUuid(@PathVariable("id") String id){
		return operationGoodsTakeService.getUuid(id);
	}
}
